<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: Z-7
  Date: 2022/3/1
  Time: 12:07
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="${pageContext.request.contextPath}/resource/bootstrap4/css/bootstrap.css" rel="stylesheet">
    <script type="text/javascript" src="${pageContext.request.contextPath}/resource/js/jquery-3.2.1.js"></script>
    <script src="${pageContext.request.contextPath}/resource/bootstrap4/js/bootstrap.js"></script>
    <title>豪哥商城后台管理</title>
</head>
<body>
<jsp:include page="head.jsp"/>
    <%--主要操作部分--%>
<form action="${pageContext.request.contextPath}/goods/list" method="post">
    <input type="hidden" name="pageNum" value="${page.pageNum}">
</form>
    <div class="container">
        <div class="row">
            <c:forEach items="${page.list}" var="good">
                <div class="card col-3" style="margin-right:70px;margin-top:5px;width: 18rem;">
                    <img src="/img/${good.picture}" width="200px" height="220px" class="card-img-top">
                    <div class="card-body">
                        <h5 class="card-title">${good.name}</h5>
                        <p class="card-text">${good.title}</p>
                        <p class="card-text">${good.brandName},${good.catName}</p>
                        <p class="card-text">${good.price}</p>
                        <button type="button" class="btn btn-outline-primary" onclick="addCart(${good.id})">加入购物车</button>
                    </div>
                </div>
            </c:forEach>
        </div>
        <div class="row">
            <button type="button" class="btn btn-outline-primary" onclick="goPage(1)">首页</button>
            <button type="button" class="btn btn-outline-primary" onclick="goPage(${page.pageNum == 1 ? 1 : page.pageNum - 1 })">上一页</button>
            <button type="button" class="btn btn-outline-primary" onclick="goPage(${page.pageNum == page.pages ? page.pages : page.pageNum + 1 })">下一页</button>
            <button type="button" class="btn btn-outline-primary" onclick="goPage(${page.pages})">尾页</button>
        </div>
    </div>
    <nav class="navbar navbar-default navbar-fixed-bottom container bg-light">
        关于我们
    </nav>
<script type="text/javascript">
    function addCart(id) {
        $.post("${pageContext.request.contextPath}/carts/addCarts",{id:id},function (obj) {
            if(obj.code == "200"){
                alert("加入成功");
                return;
            }else if(obj.code == "500"){
                alert(obj.errorInfo);
                location="${pageContext.request.contextPath}/user/login";
            }
            alert("加入失败");
            return;
        })
    }
    function goPage(pageNum) {
        $('[name="pageNum"]').val(pageNum);
        $("form").submit();
    }
</script>
</body>
</html>
